package com.boat.sample.ddd.interfaces.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RestController;

import com.boat.framework.base.api.Response;
import com.boat.sample.ddd.api.controller.UserApi;
import com.boat.sample.ddd.api.dto.FindAccountInfoRsp;
import com.boat.sample.ddd.api.dto.FindUserRsp;
import com.boat.sample.ddd.api.dto.LoginReq;
import com.boat.sample.ddd.api.dto.LoginRsp;
import com.boat.sample.ddd.api.dto.RegisterReq;
import com.boat.sample.ddd.api.dto.RegisterRsp;
import com.boat.sample.ddd.application.service.UserAppService;
import com.boat.sample.ddd.domain.system.entity.User;
import com.boat.sample.ddd.domain.system.repository.UserRepository;
import com.boat.sample.ddd.infrastructure.client.SupportClient;
import com.boat.sample.ddd.interfaces.assembler.UserAssembler;
import com.boat.starter.satoken.utils.SaTokenUtils.LoginResult;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class UserController implements UserApi {
	@Autowired
	UserAppService userAppService;
	@Autowired
	UserRepository userRepository;
	@Autowired
	SupportClient supportClient;

	@Override
	public Response<RegisterRsp> register(RegisterReq request) {
		User user = UserAssembler.toUser(request);
		LoginResult loginResult =  userAppService.register(user);
		return Response.ok(new RegisterRsp(loginResult.getToken(),loginResult.getTimeout()));
	}
	
	@Override
	public Response<LoginRsp> login(@Valid LoginReq request) {
		LoginResult loginResult =  userAppService.login(request);
		return Response.ok(new LoginRsp(loginResult.getToken(),loginResult.getTimeout()));
	}

	@Override
	//@Cached(name = "user", key = "#userId", cacheType = CacheType.REMOTE, expire = 60)
	//@AdmAuthLogin
	//@AdmAuthUser("admin")
	//@AdmAuthRole("admin")
	//@AdmAuthPerm(name="菜单管理", value="system:menu:list")
	@Cacheable(cacheNames = "UserC",key = "#userId")
	public Response<FindUserRsp> findUser(String userId) {
		User user = userAppService.findUser(userId);
		return Response.ok(UserAssembler.toFindUserRsp(user));
	}

	@Override
	public Response<FindAccountInfoRsp> findAccountInfo(String userId) {
		
		return Response.ok();
//		AccountInfoRsp accountInfoRsp = supportClient.accountInfo(userId);
//		return Response.ok(UserAssembler.toFindAccountInfoRsp(accountInfoRsp));
	}

	

}
